package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.PmFaults;
import com.ruoyi.system.domain.Workorders;
import com.ruoyi.system.mapper.PmFaultsMapper;
import com.ruoyi.system.mapper.WorkordersMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PmRepairOrdersMapper;
import com.ruoyi.system.domain.PmRepairOrders;
import com.ruoyi.system.service.IPmRepairOrdersService;

/**
 * 维修接单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-25
 */
@Service
public class PmRepairOrdersServiceImpl implements IPmRepairOrdersService {
    @Autowired
    private PmRepairOrdersMapper pmRepairOrdersMapper;
    @Autowired
    private WorkordersMapper workordersMapper;
    @Autowired
    private PmFaultsMapper pmFaultsMapper;

    /**
     * 查询维修接单
     *
     * @param id 维修接单主键
     * @return 维修接单
     */
    @Override
    public PmRepairOrders selectPmRepairOrdersById(Long id) {
        return pmRepairOrdersMapper.selectPmRepairOrdersById(id);
    }

    /**
     * 查询维修接单列表
     *
     * @param pmRepairOrders 维修接单
     * @return 维修接单
     */
    @Override
    public List<PmRepairOrders> selectPmRepairOrdersList(PmRepairOrders pmRepairOrders) {
//        Long userId = SecurityUtils.getUserId();
//        pmRepairOrders.setCreatorId(userId);
        return pmRepairOrdersMapper.selectPmRepairOrdersList(pmRepairOrders);
    }

    /**
     * 新增维修接单
     *
     * @param pmRepairOrders 维修接单
     * @return 结果
     */
    @Override
    public int insertPmRepairOrders(PmRepairOrders pmRepairOrders) {
        Workorders workorders = workordersMapper.selectWorkordersById(pmRepairOrders.getWorkOrderId());
        if (ObjectUtils.isEmpty(workorders)) {
            throw new ServiceException("工单查询错误");
        }
        PmFaults pmFaults = pmFaultsMapper.selectPmFaultsByFaultId(workorders.getFaultId());
        if (ObjectUtils.isEmpty(pmFaults)) {
            throw new ServiceException("故障查询错误");
        }
        workorders.setStatus("3");
        workordersMapper.updateWorkorders(workorders);
        pmFaults.setStatus("3");
        pmFaultsMapper.updatePmFaults(pmFaults);
        BeanUtils.copyProperties(pmFaults, pmRepairOrders);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        pmRepairOrders.setCreatorId(loginUser.getUserId());
        pmRepairOrders.setCreatorName(loginUser.getUsername());
        pmRepairOrders.setWorkCreatorName(workorders.getCreatorName());
        pmRepairOrders.setRepairDate(new Date());
        return pmRepairOrdersMapper.insertPmRepairOrders(pmRepairOrders);
    }

    /**
     * 修改维修接单
     *
     * @param pmRepairOrders 维修接单
     * @return 结果
     */
    @Override
    public int updatePmRepairOrders(PmRepairOrders pmRepairOrders) {
        if (ObjectUtils.isEmpty(pmRepairOrders.getSchedule())) {
            throw new ServiceException("完成进度不能为空");
        }
        PmRepairOrders orders = pmRepairOrdersMapper.selectPmRepairOrdersById(pmRepairOrders.getId());
        if (100 == pmRepairOrders.getSchedule()) {
            Workorders workorders1 = workordersMapper.selectWorkordersById(orders.getWorkOrderId());
            PmFaults pmFaults = new PmFaults();
            pmFaults.setFaultId(workorders1.getFaultId());
            pmFaults.setStatus("4");
            pmFaultsMapper.updatePmFaults(pmFaults);
            pmRepairOrders.setCompletionDate(new Date());
            Workorders workorders = new Workorders();
            workorders.setStatus("4");
            workorders.setId(orders.getWorkOrderId());
            workordersMapper.updateWorkorders(workorders);
        }
        return pmRepairOrdersMapper.updatePmRepairOrders(pmRepairOrders);
    }

    /**
     * 批量删除维修接单
     *
     * @param ids 需要删除的维修接单主键
     * @return 结果
     */
    @Override
    public int deletePmRepairOrdersByIds(Long[] ids) {
        return pmRepairOrdersMapper.deletePmRepairOrdersByIds(ids);
    }

    /**
     * 删除维修接单信息
     *
     * @param id 维修接单主键
     * @return 结果
     */
    @Override
    public int deletePmRepairOrdersById(Long id) {
        return pmRepairOrdersMapper.deletePmRepairOrdersById(id);
    }
}
